|
-- This module implements in Lua, and adds a few -- new features. -- Process a numeric argument to make sure it is a positive -- integer. local function processNumArg( num ) if num then num = tonumber( num ) if type( num ) == 'number' then num = math.floor( num ) if num >= 0 then return num end end end return nil end -- Checks whether a page exists, going through pcall -- in case we are over the expensive function limit. local function checkPageExists( title ) if not title then error('No title passed to checkArchiveExists', 2) end local noError, titleObject = pcall(mw.title.new, title) if not noError then -- If we are over the expensive function limit then assume -- that the page doesn't exist. return false else if titleObject then return titleObject.exists else return false -- Return false if given a bad title. end end end -- Checks every nth archive to see if it exists, and returns the -- number of the first archive that doesn't exist. It is -- necessary to do this in batches because each check is an -- expensive function call, and we want to avoid making too many -- of them so as not to go over the expensive function limit. local function checkArchives( prefix, n, start ) local i = start local exists = true while exists do exists = checkPageExists( prefix .. tostring( i ) ) if exists then i = i + n end end return i end -- Return the biggest archive number, using checkArchives() -- and starting in intervals of 1000. This should get us a -- maximum of 500,000 possible archives before we hit the -- expensive function limit. local function getBiggestArchiveNum( prefix, max ) -- Return the value for max if it is specified. max = processNumArg( max ) if max then return max end -- Otherwise, detect the largest archive number. local check1000 = checkArchives( prefix, 1000, 1 ) if check1000 == 1 then return 0 -- Return 0 if no archives were found. end local check200 = checkArchives( prefix, 200, check1000 - 1000 ) local check50 = checkArchives( prefix, 50, check200 - 200 ) local check10 = checkArchives( prefix, 10, check50 - 50 ) local check1 = checkArchives( prefix, 1, check10 - 10 ) -- check1 is the first page that doesn't exist, so we want to -- subtract it by one to find the biggest existing archive. return check1 - 1 end -- Get the archive link prefix (the title of the archive pages -- minus the number). local function getPrefix( root, prefix, prefixSpace ) local ret = root or mw.title.getCurrentTitle().prefixedText ret = ret .. '/' if prefix then ret = ret .. prefix if prefixSpace == 'yes' then ret = ret .. ' ' end else ret = ret .. '過去ログ' end return ret end -- Get the number of archives to put on one line. Set to -- math.huge if there should be no line breaks. local function getLineNum( links, nobr, isLong ) local linksToNum = tonumber( links ) local lineNum if nobr == 'yes' or (links and not linksToNum) then lineNum = math.huge -- If links is a number, process it. Negative values and expressions -- such as links=8/2 produced some interesting values with the old -- template, but we will ignore those for simplicity. elseif type(linksToNum) == 'number' and linksToNum >= 0 then -- The old template rounded down decimals to the nearest integer. lineNum = math.floor( linksToNum ) if lineNum == 0 then -- In the old template, values of links between 0 and 0.999 -- suppressed line breaks. lineNum = math.huge end else 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「モジュール:Archive list」の詳細全文を読む スポンサード リンク
|